﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LXD.StudnetMangement.Models
{
    public class MockStudentRepository : IStudentRepository
    {

        private List<Student> _students;


        public MockStudentRepository()
        {
            _students = new List<Student>()
            {
                new Student(){ID=1,Name="张三",ClassName=ClassNameEnum.FirstGrade,Email="Tony@qq.com",Major="计算机科学"},
                new Student(){ID=2,Name="李四",ClassName=ClassNameEnum.SecondGrade,Email="lisi@qq.com",Major="物流"},
                new Student(){ID=3,Name="王二",ClassName=ClassNameEnum.GradeThree,Email="wang@qq.com", Major="美术"},
            };
        }

        public Student Add(Student student)
        {
            student.ID = _students.Max(x => x.ID) + 1;
            _students.Add(student);
            return student;
        }

        public Student Del(int id)
        {
            Student item = _students.SingleOrDefault(x => x.ID == id);
            if (item != null)
            {
                _students.Remove(item);
            }
            return item;
        }

        public IEnumerable<Student> GetAllStudents()
        {
            return _students;
        }

        public Student GetStudent(int id)
        {
            return _students.FirstOrDefault(a => a.ID == id);
        }

        public Student Update(Student updateStudent)
        {
            Student student = _students.SingleOrDefault(s => s.ID == updateStudent.ID);
            if (student != null)
            {
                student.Name = updateStudent.Name;
                student.Email = updateStudent.Email;
                student.ClassName = updateStudent.ClassName;
            }
            return student;
        }
    }
}
